﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using nRoute.Components;
using nRoute.Samples.FutureDesktop.Views;
using nRoute.ViewModels;

namespace nRoute.Samples.FutureDesktop.ViewModels
{
    [MapViewModel(typeof(FuturePage))]
    public class FuturePageViewModel 
        : NavigationViewModelBase
    {

#region Constants and Variables

        private const string COLOR_KEY = "UserColor__KEY";
        private const string TEXT_KEY = "UserText__KEY";
        private const string PAGE_NAME_KEY = "Name";

        private int _userColor;
        private string _userText;

#endregion

        public FuturePageViewModel()
        {
            // we set up the action command
            ChangeColourCommand = new ActionCommand(() => this.UserColor = GetRandomColor());
            this.UserColor = GetRandomColor();
        }

#region Properties

        public ActionCommand ChangeColourCommand { get; private set; }

        public int UserColor
        {
            get { return _userColor; }
            set
            {
                _userColor = value;
                NotifyPropertyChanged(() => UserColor);
            }
        }

        public string UserText
        {
            get
            {
                return _userText;
            }
            set
            {
                _userText = value;
                NotifyPropertyChanged(() => UserText);
            }
        }

        public new string Title
        {
            get { return base.Title;  }
            set { base.Title = value;  }
        }

#endregion

#region ISupportInitializeState Members

        protected override void OnIntialize(ParametersCollection state)
        {
            // we get the name of the page from the parameters, see the MapNavigationContent attribute
            Title = SpaceMessage(state[PAGE_NAME_KEY] as string);
        }

        protected override void OnRestoreState(ParametersCollection state)
        {
            // our dictionary is send back to us, 
            if (state != null && state.ContainsKey(TEXT_KEY))
            {
                this.UserColor = Convert.ToInt32(state[COLOR_KEY]);
                this.UserText = Convert.ToString(state[TEXT_KEY]);
            }
        }

        protected override ParametersCollection OnSaveState()
        {   
            var _parametersCollection = new ParametersCollection();
            _parametersCollection.Add(TEXT_KEY, this.UserText);
            _parametersCollection.Add(COLOR_KEY, this.UserColor);
            return _parametersCollection;
        }

#endregion

#region Helpers

        private static int GetRandomColor()
        {
            var _random = new Random(DateTime.Now.Millisecond);
            return _random.Next(0, 8);      // --> 0 to 8, nine colours are available
        }

        private static string SpaceMessage(string message)
        {
            var _chars = message.ToCharArray().Where(Char.IsUpper).ToList();
            foreach (var _char in _chars)
            {
                message = message.Replace(_char.ToString(), " " + _char);
            }
            return message.Trim().Replace("  ", " ");
        }

#endregion

    }
}
